/*
* License:  This  program  is  free  software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by
* the  Free Software Foundation; either version 3 of the License, or (at your
* option)  any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*/

package pcgen.CharacterViewer.utility;

import java.util.ArrayList;
import java.util.Collections;

import org.apache.commons.lang.StringUtils;

import pcgen.ChainedComparator;

import pcgen.CharacterViewer.fragments.CharacterTabContentFragment;
import pcgen.CharacterViewer.resources.GameFolder;
import pcgen.CharacterViewer.resources.GameResourceAttributeAbility;
import pcgen.CharacterViewer.resources.GameResourceAttributeAttack;
import pcgen.CharacterViewer.resources.GameResourceAttributeSkill;
import pcgen.CharacterViewer.resources.GameResourceCharacter;
import pcgen.CharacterViewer.resources.GameResourceCondition;
import pcgen.CharacterViewer.resources.GameResourceWeapon;

public class GameResourceCharacterJavascriptInterface
{
	public GameResourceCharacterJavascriptInterface(CharacterTabContentFragment fragment, GameResourceCharacter resource)
	{
		_fragment = fragment;
		_character = resource;
	}
	
	public String getAbilityStat(int index)
	{
		return String.valueOf(getCharacter().getAbilityById(index).getValue());
	}
	
	public String getAbilityStatOriginal(int index)
	{
		return String.valueOf(getCharacter().getAbilityById(index).getValueOriginal());
	}
	
	public String getAbilityStatColor(int index)
	{
		return getColor(getCharacter().getAbilityById(index).getModifierTotal());
	}
	
	public String getAbilityStatMod(int index)
	{
		return valueOf(getCharacter().getAbilityById(index).getAbilityModifier());
	}
	
	public String getAbilityStatDamage(int index)
	{
		return valueOf(getCharacter().getAbilityById(index).getModifierDamageAndHealing());
	}
	
	public String getAbilityStatBonus(int index)
	{
		return valueOf(getCharacter().getAbilityById(index).getModifierTemp());
	}
	
	public String getAbilitySave(int index)
	{
		return valueOf(getCharacter().getSaveById(index).getValue());
	}
	
	public String getAbilitySaveStat(int index)
	{
		return valueOf(getCharacter().getSaveById(index).getAbilityModifier());
	}
	
	public String getAbilitySaveMisc(int index)
	{
		return valueOf(getCharacter().getSaveById(index).getMisc());
	}
	
	public String getAbilitySaveTemp(int index)
	{
		return valueOf(getCharacter().getSaveById(index).getModifier());
	}
	
	public String getAttackBase()
	{
		return valueOf(getCharacter().getAttackMelee().getValue());
	}
	
	public String getAttackBaseIterative()
	{
		return getCharacter().getAttackMelee().getBaseAttackBonusIterative();
	}
	
	public String getAttackBaseIterativeColor(GameResourceAttributeAttack attack)
	{
		if (attack == null)
			return "Black";
		
		return getColor(attack.getModifierTotal());
	}
	
	public String getAttackBaseGrapple()
	{
		return valueOf(getCharacter().getAttackGrapple().getValue());
	}
	
	public String getAttackBaseGrappleIterative()
	{
		return getCharacter().getAttackGrapple().getValueIterative();
	}
	
	public String getAttackBaseGrappleIterativeColor()
	{
		return getAttackBaseIterativeColor(getCharacter().getAttackGrapple());
	}
	
	public String getAttackBaseGrappleStat()
	{
		return valueOf(getCharacter().getAttackGrapple().getAbilityModifier());
	}
	
	public String getAttackBaseGrappleTemp()
	{
		return valueOf(getCharacter().getAttackGrapple().getModifier());
	}
	
	public String getAttackBaseMelee()
	{
		return valueOf(getCharacter().getAttackMelee().getValue());
	}
	
	public String getAttackBaseMeleeIterative()
	{
		return getCharacter().getAttackMelee().getValueIterative();
	}
	
	public String getAttackBaseMeleeIterativeColor()
	{
		return getAttackBaseIterativeColor(getCharacter().getAttackMelee());
	}
	
	public String getAttackBaseMeleeStat()
	{
		return valueOf(getCharacter().getAttackMelee().getAbilityModifier());
	}
	
	public String getAttackBaseMeleeTemp()
	{
		return valueOf(getCharacter().getAttackMelee().getModifier());
	}
	
	public String getAttackBaseRanged()
	{
		return valueOf(getCharacter().getAttackRanged().getValue());
	}
	
	public String getAttackBaseRangedIterative()
	{
		return getCharacter().getAttackRanged().getValueIterative();
	}
	
	public String getAttackBaseRangedIterativeColor()
	{
		return getAttackBaseIterativeColor(getCharacter().getAttackRanged());
	}
	
	public String getAttackBaseRangedStat()
	{
		return valueOf(getCharacter().getAttackRanged().getAbilityModifier());
	}
	
	public String getAttackBaseRangedTemp()
	{
		return valueOf(getCharacter().getAttackRanged().getModifier());
	}
	
	public String getAttackBaseUnarmed()
	{
		return valueOf(getCharacter().getAttackUnarmed().getValue());
	}
	
	public String getAttackBaseUnarmedIterative()
	{
		return getCharacter().getAttackUnarmed().getValueIterative();
	}
	
	public String getAttackBaseUnarmedIterativeColor()
	{
		return getAttackBaseIterativeColor(getCharacter().getAttackUnarmed());
	}
	
	public String getAttackBaseUnarmedDamage()
	{
		return getCharacter().getAttackUnarmed().getDamage();
	}
	
	public String getAttackBaseUnarmedDamageColor(int index)
	{
		return getColor(getCharacter().getAttackUnarmed().getModifierTotal());
	}
	
	public String getAttackWeapon(int weapon)
	{
		return valueOf(getCharacter().getWeapon(weapon).getValue());
	}
	
	public String getAttackWeaponIterative(int weapon)
	{
		return getCharacter().getWeapon(weapon).getValueIterative();
	}
	
	public String getAttackWeaponIterativeColor(int weapon)
	{
		return getAttackBaseIterativeColor(getCharacter().getWeapon(weapon));
	}
	
	public String getAttackWeaponDamage(int weapon)
	{
		return getCharacter().getWeapon(weapon).getDamage();
	}
	
	public String getAttackWeaponDamageColor(int index)
	{
		GameResourceWeapon weapon = getCharacter().getWeapon(index);
		if (weapon == null)
			return "Black";
		
		return getColor(weapon.getModifierTotal());
	}
	
	public String getAttackWeaponOffHand(int weapon)
	{
		return valueOf(getCharacter().getWeapon(weapon).getValueOffHand());
	}
	
	public String getAttackWeaponOffHandIterative(int weapon)
	{
		return getCharacter().getWeapon(weapon).getValueOffHandIterative();
	}
	
	public String getAttackWeaponDamageOffHand(int weapon)
	{
		return getCharacter().getWeapon(weapon).getDamageOffHand();
	}
	
	public String getAttackWeaponTwoHanded(int weapon)
	{
		return valueOf(getCharacter().getWeapon(weapon).getValueTwoHanded());
	}
	
	public String getAttackWeaponTwoHandedIterative(int weapon)
	{
		return getCharacter().getWeapon(weapon).getValueTwoHandedIterative();
	}
	
	public String getAttackWeaponDamageTwoHanded(int weapon)
	{
		return getCharacter().getWeapon(weapon).getDamageTwoHanded();
	}
	
	public String getAttackWeaponTwoWeapon(int weapon)
	{
		return valueOf(getCharacter().getWeapon(weapon).getValueTwoWeapon());
	}
	
	public String getAttackWeaponTwoWeaponIterative(int weapon)
	{
		return getCharacter().getWeapon(weapon).getValueTwoWeaponIterative();
	}
	
	public String getAttackWeaponTwoWeaponOffHand(int weapon)
	{
		return valueOf(getCharacter().getWeapon(weapon).getValueTwoWeaponOffHand());
	}
	
	public String getAttackWeaponTwoWeaponOffHandIterative(int weapon)
	{
		return getCharacter().getWeapon(weapon).getValueTwoWeaponOffHandIterative();
	}
	
	public String getAttackWeaponDamageTwoWeaponOffHand(int weapon)
	{
		return getCharacter().getWeapon(weapon).getDamageTwoWeaponOffHand();
	}
	
	public String getAttackWeaponTwoWeaponSecondaryLight(int weapon)
	{
		return valueOf(getCharacter().getWeapon(weapon).getValueTwoWeaponSecondaryLight());
	}
	
	public String getAttackWeaponTwoWeaponSecondaryLightIterative(int weapon)
	{
		return getCharacter().getWeapon(weapon).getValueTwoWeaponSecondaryLightIterative();
	}
	
	public String getAttackWeaponDamageTwoWeaponSecondaryLight(int weapon)
	{
		return getCharacter().getWeapon(weapon).getDamageTwoWeaponSecondaryLight();
	}
	
	public String getAttackWeaponRanged(int weapon)
	{
		return valueOf(getCharacter().getWeapon(weapon).getValue());
	}
	
	public String getAttackWeaponRangedIterative(int weapon, int range)
	{
		return getCharacter().getWeapon(weapon).getValueRangeIterative(range);
	}
	
	public String getAttackWeaponRangedIterativeColor(int weapon)
	{
		return getAttackBaseIterativeColor(getCharacter().getWeapon(weapon));
	}
	
	public String getAttackWeaponRangedDamage(int weapon)
	{
		return getCharacter().getWeapon(weapon).getDamage();
	}
	
	@SuppressWarnings("unchecked")
	public String getConditionalModifiers()
	{
		StringBuilder output = new StringBuilder();

		ArrayList<GameResourceCondition> list = getCharacter().getConditionalModifiers();
		Collections.sort(list, new ChainedComparator<GameResourceCondition>(new GameResourceConditionSortOrder(_fragment), new GameResourceConditionSortName(_fragment)));
		
		for(GameResourceCondition condition : list)
		{
			String color = getFolder().getTypeConditionImportance(condition.getConditionType());
			output.append("<div");
			if (!StringUtils.isEmpty(color))
				output.append(" style=\"color:").append(color).append("\"");
			output.append(">");
			output.append(_fragment.getString(condition.getName()));
			output.append("</div>");
		}
		return output.toString();
	}
	
	public String getDamagePrimary()
	{
		return String.valueOf(getCharacter().getDamagePrimaryCurrent());
	}
	
	public String getDamagePrimaryColor()
	{
		if (getCharacter().getDamagePrimaryCurrent() <= getCharacter().getDamagePrimaryMin())
			return "DarkRed ";
		
		if (getCharacter().getDamagePrimaryCurrent() == 0)
			return "Red";
		
		if (getCharacter().getDamagePrimaryCurrent() <= (getCharacter().getDamagePrimaryMax() / 2))
			return "OrangeRed";
		
		double percent = getCharacter().getDamagePrimaryMax() * 0.95;
		if (getCharacter().getDamagePrimaryCurrent() < percent)
			return "Orange";
		
		return "Green";
	}
	
	public String getDamagePrimaryMax()
	{
		return String.valueOf(getCharacter().getDamagePrimaryMax());
	}
	
	public String getDamagePrimaryMin()
	{
		return String.valueOf(getCharacter().getDamagePrimaryMin());
	}
	
	public String getDamagePrimaryTemp()
	{
		return valueOf(getCharacter().getDamagePrimaryTemp());
	}
	
	public String getDamageSecondary()
	{
		return String.valueOf(getCharacter().getDamageSecondaryCurrent());
	}
	
	public String getDamageSecondaryColor()
	{
		if (getCharacter().getFolder().getSettingsDamageTypeVitalityPoints())
		{
    		if (getCharacter().getDamageSecondaryCurrent() <= getCharacter().getDamageSecondaryMin())
    			return "DarkRed ";
    		
    		if (getCharacter().getDamageSecondaryCurrent() == 0)
    			return "Red";
    		
    		if (getCharacter().getDamageSecondaryCurrent() <= (getCharacter().getDamageSecondaryMax() / 2))
    			return "OrangeRed";
    		
    		if (getCharacter().getDamageSecondaryCurrent() < getCharacter().getDamageSecondaryMax())
    			return "Orange";
    		
    		double percent = getCharacter().getDamageSecondaryMax() * 0.95;
    		if (getCharacter().getDamageSecondaryCurrent() < percent)
    			return "Orange";
		}
		else
		{
			if (getCharacter().getDamageSecondaryCurrent() == getCharacter().getDamageSecondaryMax())
    			return "Red";
    		
    		if (getCharacter().getDamageSecondaryCurrent() >= (getCharacter().getDamageSecondaryMax() / 2))
    			return "OrangeRed";
    		
    		double percent = getCharacter().getDamageSecondaryMin() * 0.95;
    		if (getCharacter().getDamageSecondaryCurrent() < percent)
    			return "Orange";
		}
		
		return "Green";
	}
	
	public String getDamageSecondaryMax()
	{
		return String.valueOf(getCharacter().getDamageSecondaryMax());
	}
	
	public String getDamageSecondaryMin()
	{
		return String.valueOf(getCharacter().getDamageSecondaryMin());
	}
	
	public String getDamageSecondaryTemp()
	{
		return valueOf(getCharacter().getDamageSecondaryTemp());
	}
	
	public int getDebugMode()
	{
		return (_fragment.getApplication().getDebugMode() ? 1: 0);
	}
	
	public String getDefenseFlatFooted()
	{
		return String.valueOf(getCharacter().getDefense().getValueFlatFooted());
	}
	
	public String getDefenseShieldless()
	{
		return String.valueOf(getCharacter().getDefense().getValueShieldless());
	}
	
	public String getDefenseStat()
	{
		return valueOf(getCharacter().getAbilityDex().getAbilityModifier());
	}
	
	public String getDefenseTotal()
	{
		return String.valueOf(getCharacter().getDefense().getValue());
	}
	
	public String getDefenseTouch()
	{
		return String.valueOf(getCharacter().getDefense().getValueTouch());
	}
	
	public String getDefenseTemp()
	{
		return valueOf(getCharacter().getDefense().getModifier());
	}
	
	public String getInitiative()
	{
		return valueOf(getCharacter().getInitiative());
	}
	
	public String getInitiativeColor()
	{
		return "Green";
	}
	
	public String getInitiativeModifier()
	{
		return valueOf(getCharacter().getInitiativeModifier());
	}
	
	public String getInitiativeStat()
	{
		return valueOf(getCharacter().getInitiativeStatModifier());
	}
	
	public String getSkill(int index)
	{
		return valueOf(getCharacter().getSkill(index).getValue());
	}
	
	public String getSkillColor(int index)
	{
		GameResourceAttributeSkill skill = getCharacter().getSkill(index);
		if (skill == null)
			return "Black";
		
		return getColor(skill.getModifierTotal());
	}
	
	public String getSkillMisc(int index)
	{
		return valueOf(getCharacter().getSkill(index).getMisc());
	}
	
	public String getSkillTemp(int index)
	{
		return valueOf(getCharacter().getSkill(index).getModifier());
	}
	
	public String getSkillStat(int index)
	{
		return valueOf(getCharacter().getSkill(index).getAbilityModifier());
	}
	
	private GameResourceCharacter getCharacter()
	{
		return _character;
	}
	
	private String getColor(int value)
	{
		return getColor(0, value);
	}
	
	private String getColor(int value1, int value2)
	{
		if (value1 > value2)
			return "Red";
		else if (value1 < value2)
			return "Green";
		
		return "Black";
	}
	
	private GameFolder getFolder()
	{
		return (_character != null ? _character.getFolder() : null);
	}
	
	private String valueOf(int value)
	{
		String output = String.valueOf(value);
		if (!output.startsWith("-") && !output.startsWith("+"))
			output = "+" + output;
		return output;
	}
	
	private CharacterTabContentFragment _fragment;
	private GameResourceCharacter _character;
}
